@echo off
setlocal

REM UTF8编码
chcp 65001

git config --global user.name 'awn-tseng'
git config --global user.email 'zengwen.chu@qq.com'
git config --global user.password "QADLPWYYAN520"

REM 设置GitHub仓库URL和目标目录
REM set REPO_URL=https://zedweein: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@github.com/zedweein/scm.git
set REPO_URL=https://gitee.com/awn-tseng/scm.git

set TARGET_DIR=C:\scm\source\scm
set OUTPUT_DIR=C:\scm

REM 查找Java进程和子进程并kill
for /f "tokens=2 delims=," %%i in ('tasklist /nh /fi "imagename eq java.exe" /fo csv') do (
	echo Killing Java Process With PID %%i
	taskkill /F /PID %%i /T > nul 2>&1
)

REM 克隆GitHub仓库到指定目录
rmdir /s /q "%TARGET_DIR%"
echo Removing "%TARGET_DIR%"

if not exist "%TARGET_DIR%" mkdir "%TARGET_DIR%"
cd /d "%TARGET_DIR%"
git clone %REPO_URL% .

REM 检查是否成功克隆了仓库
if errorlevel 1 (
    echo Git Clone Failed. Aborting.
    goto :EOF
)

REM 执行mvn clean package
call mvn clean package

REM 检查是否成功编译了项目
if errorlevel 1 (
    echo Maven Build Failed. Aborting.
    goto :EOF
)

REM 找到最新的JAR文件
for /f "delims=|" %%F in ('dir /b /o-d target\*.jar') do set NEWEST_JAR=%%F

REM 复制scm.jar到scm目录
xcopy /y "target\%NEWEST_JAR%" "%OUTPUT_DIR%\"
if errorlevel 1 (
	echo Copying Scm Jar Failed. Aborting.
	goto :EOF
)

REM 复制library目录到scm目录
xcopy /y "target\library" "%OUTPUT_DIR%\library\"
if errorlevel 1 (
	echo Copying Library Failed. Aborting.
	goto :EOF
)

REM 启动JAR文件
cd /d %~dp0
java -jar scm.jar C:\scm\config\config.json

endlocal
exit
